//
//  ZLJDrawerViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/8.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "ZLJDrawerViewController.h"

#define SCREEN_WIDTH CGRectGetWidth(self.view.frame)
#define SCREEN_HEIGHT CGRectGetHeight(self.view.frame)

@interface ZLJDrawerViewController ()

@property (nonatomic, strong) UIViewController *mainViewController;
@property (nonatomic, strong) UIViewController *leftViewController;
@property (nonatomic, strong) UIViewController *rightViewController;

@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *leftDrawerOpenGesture;

@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UITapGestureRecognizer *backMainViewGesture;

@property (nonatomic, assign) BOOL isAnimationOfDrawer;
@property (nonatomic, assign) BOOL drawerViewIsOpen;

- (void)initialDrawerViewController;
- (void)controlDrawerOfLeftOrRifht:(BOOL)isLeft open:(BOOL)isOpen;

@end

@implementation ZLJDrawerViewController

#pragma mark -- initialize

- (instancetype)initWithMainViewController:(UIViewController *)mainViewController leftViewController:(UIViewController *)leftViewController rightViewController:(UIViewController *)rightViewController {
    self = [super init];
    if (self) {
        self.mainViewController = mainViewController;
        self.leftViewController = leftViewController;
        self.rightViewController = rightViewController;
    }
    return self;
}

- (instancetype)initWithMainViewController:(UIViewController *)mainViewController leftViewController:(UIViewController *)leftViewController {
    return [self initWithMainViewController:mainViewController leftViewController:leftViewController rightViewController:nil];
}

#pragma mark - lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialDrawerViewController];
}

#pragma mark - private methods
- (void)initialDrawerViewController {
    //load leftDrawer
    [self addChildViewController:self.leftViewController];
    [self.leftViewController didMoveToParentViewController:self];

    
    //load rightDrawer
    if (self.rightViewController) {
        [self addChildViewController:self.rightViewController];
        [self.rightViewController didMoveToParentViewController:self];
    }
    //load mainDrawer
    [self addChildViewController:self.mainViewController];
    [self.mainViewController didMoveToParentViewController:self];
    [self.view addSubview:self.mainViewController.view];
    [self.view addGestureRecognizer:self.leftDrawerOpenGesture];
    
}

- (void)controlDrawerOfLeftOrRifht:(BOOL)isLeft open:(BOOL)isOpen {
    CGRect frame;
    void (^completionBlock)(BOOL) = nil;
    if (isLeft && isOpen) {
        //open leftDrawer
        frame = CGRectMake(300, 0, 375, 667);
        [self.view insertSubview:self.leftViewController.view belowSubview:self.mainViewController.view];
        [self.mainViewController.view addSubview:self.coverView];
        self.drawerViewIsOpen = YES;
    } else if (isLeft && !isOpen) {
        //close leftDrawer
        frame = self.view.bounds;
        [self.coverView removeFromSuperview];
        completionBlock = ^(BOOL flag) {
            [self.leftViewController.view removeFromSuperview];
        };
        self.drawerViewIsOpen = NO;
    } else if (!isLeft && isOpen) {
        //open rightDrawer
        frame = CGRectMake(-300, 0, 375, 667);
        [self.view insertSubview:self.rightViewController.view belowSubview:self.mainViewController.view];
    } else if (!isLeft && !isOpen){
        //close rightDrawer
        frame = self.view.bounds;
        completionBlock = ^(BOOL flag) {
            [self.rightViewController.view removeFromSuperview];
        };
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.mainViewController.view.frame = frame;
    } completion:completionBlock];
}

#pragma mark - interface methods
- (void)openLeftDrawer {
    [self controlDrawerOfLeftOrRifht:YES open:YES];
}

- (void)closeLeftDrawer {
    [self controlDrawerOfLeftOrRifht:YES open:NO];
}

- (void)openRightDrawer {
    [self controlDrawerOfLeftOrRifht:NO open:YES];
}

- (void)closeRightDrawer {
    [self controlDrawerOfLeftOrRifht:NO open:NO];
}
#pragma mark - callback / action
- (void)gestureOfOpenDrawer:(UIScreenEdgePanGestureRecognizer *)gesture {
    [self openLeftDrawer];
    
}
- (void)backMainViewGestureTaped:(UITapGestureRecognizer *)gesture {
    [self closeLeftDrawer];
}


#pragma mark - getter 
- (UIScreenEdgePanGestureRecognizer *)leftDrawerOpenGesture {
    if (!_leftDrawerOpenGesture) {
        _leftDrawerOpenGesture = ({
            UIScreenEdgePanGestureRecognizer *gesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureOfOpenDrawer:)];
            gesture.edges = UIRectEdgeLeft;
            gesture;
        });
    }
    return _leftDrawerOpenGesture;
}

- (UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] init];
        _coverView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.2;
        [_coverView addGestureRecognizer:self.backMainViewGesture];
    }
    return _coverView;
}

- (UITapGestureRecognizer *)backMainViewGesture {
    if (!_backMainViewGesture) {
        _backMainViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backMainViewGestureTaped:)];
    }
    return _backMainViewGesture;
}




@end
